package servlet.manager;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import javabean.JDBCBean;
import net.sf.json.JSONObject;

@WebServlet("/manager/orderaccept")
public class OrderAccept extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json; charset=utf8");
        String id = req.getParameter("id");
        HttpSession session=req.getSession();
        String Pid = session.getAttribute("manager").toString();
        JSONObject json = new JSONObject();
        JDBCBean db = new JDBCBean();

        String sql = "update "+Pid+"delivery "+"set state=1 where Oid='"+id+"'";
        String sql2 = "update oorder set state='transporting' where Oid='"+id+"' ";
        String sqlchack ="SELECT COUNT(*) FROM oorder WHERE Oid ='" + id  +"' and state = 'no-transported' or state ='no-propelled' ";
        String sqlchack2 ="select * from "+Pid+"delivery  where Oid ='"+id+"' ";
        //检查快递表里对应的单号是否已经被接单，如果已经被别人接走说明我的接单无效
        String charea = null;
        int check = 0;
        int result = 0;
        int code = 1;
        int state = 0;
        String msg = "";
        if( id != null && !id.equals("") ) {
            try {
                ResultSet rs = db.executeQuery(sqlchack2);
                if(rs.next()){
                    charea= rs.getString("nowarea");
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                sqlchack += " and nowarea ='"+charea+"'";
                ResultSet rs = db.executeQuery(sqlchack);
                if(rs.next()){
                    check= rs.getInt("COUNT(*)");
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            if(check!=0) //没出错
            {
                result = db.executeUpdate(sql);
                result += db.executeUpdate(sql2);

            }
            else result +=1;//出错了
            sql = "select count(*) from "+Pid+"delivery "+" where Oid= '"+id+"' and state = '0'";
            ResultSet rs=db.executeQuery(sql);
            try {
                if(rs.next()){
                    state =rs.getInt("count(*)");
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            //用于判断，如果sql语句正常执行只会返回0，返回两个0说明一切正常，否则说明出错
        }
        if( result != 1 ) {
            code = 0;
            msg = "接单成功";
        }else{
            code = 1;
            msg = "接单失败";
            if(state !=0 ){
                sql = "delete from "+Pid+"delivery "+" where Oid= '"+id+"'";
                db.executeUpdate(sql);
            }

        }
        db.close();
        json.put("code", code);
        json.put("msg", msg);
        db.close();
        PrintWriter out = resp.getWriter();
        out.print( json.toString() );

    }

}

